package cn.dglydrpy.study.j2ee.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.dglydrpy.study.j2ee.manage.dao.UserDAO;
import cn.dglydrpy.study.j2ee.manage.entity.User;



/*
 * 返回给浏览器当前用户列表
 * 把输出交给jsp （转发）
 */
public class ListUserServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			UserDAO dao = new UserDAO();
			List<User> users = dao.findAll();
			//1.绑定数据到request
			request.setAttribute("users", users);
			//2.获得转发器
			RequestDispatcher rd = request.getRequestDispatcher("listUser5.jsp");
			//3.转发
			rd.forward(request, response);
		
		} catch (Exception e) {
			e.printStackTrace();
			//转发到异常页面
			request.setAttribute("msg", "系统繁忙，稍后重试！");
			request.getRequestDispatcher("error5.jsp").forward(request, response);
			
		}
	}
}
